#include "equipmentstatus.h"
#include "dcuinformation.h"

EquipmentStatus::EquipmentStatus(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    LeftArrow = new QPushButton(this);
    LeftArrow->setText(tr("上页"));
    LeftArrow->setFont(QFont(ZITI, 20, QFont::Normal));
    LeftArrow->setGeometry(QRect(349, 360, 110, 70));
    LeftArrow->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");

    RightArrow = new QPushButton(this);
    RightArrow->setText(tr("下页"));
    RightArrow->setFont(QFont(ZITI, 20, QFont::Normal));
    RightArrow->setGeometry(QRect(471, 360, 110, 70));
    RightArrow->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");
    connect(LeftArrow, &QPushButton::clicked, this, &EquipmentStatus::devStateUISignalforUp);
    connect(RightArrow, &QPushButton::clicked, this, &EquipmentStatus::devStateUISignalforDown);
    Cancel = new QPushButton(this);
    Cancel->setText(tr("返回"));
    Cancel->setFont(QFont(ZITI, 25, QFont::Normal));
    Cancel->setGeometry(QRect(600, 360, 110, 70));
    Cancel->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");

    StatusLabel = new QLabel(this);
    StatusLabel->setText(tr("状态指示："));
    StatusLabel->setFont(QFont(ZITI, 17, QFont::Bold));
    StatusLabel->setGeometry(QRect(10, 380, 100, 40));
    StatusLabel->setStyleSheet("color:white");

    NormalBtn = new QPushButton(this);
    NormalBtn->setText(tr("正常"));
    NormalBtn->setFont(QFont(ZITI, 20, QFont::Bold));
    NormalBtn->setGeometry(QRect(120, 380, 80, 40));
    NormalBtn->setEnabled(false);
    QPalette pal = NormalBtn->palette();
    pal.setColor(QPalette::Button, Qt::green);
    pal.setColor(QPalette::ButtonText, Qt::black);
    NormalBtn->setPalette(pal);
    NormalBtn->setAutoFillBackground(true);
    NormalBtn->setFlat(true);

    ErrorBtn = new QPushButton(this);
    ErrorBtn->setText(tr("故障"));
    ErrorBtn->setFont(QFont(ZITI, 20, QFont::Bold));
    ErrorBtn->setGeometry(QRect(205, 380, 80, 40));
    ErrorBtn->setEnabled(false);
    pal = ErrorBtn->palette();
    pal.setColor(QPalette::Button, Qt::red);
    pal.setColor(QPalette::ButtonText, Qt::black);
    ErrorBtn->setPalette(pal);
    ErrorBtn->setAutoFillBackground(true);
    ErrorBtn->setFlat(true);

    UnknowBtn = new QPushButton(this);
    UnknowBtn->setText(tr("未知"));
    UnknowBtn->setFont(QFont(ZITI, 20, QFont::Bold));
    UnknowBtn->setGeometry(QRect(270, 300, 80, 40));
    UnknowBtn->setEnabled(false);
    pal = UnknowBtn->palette();
    pal.setColor(QPalette::Button, Qt::gray);
    pal.setColor(QPalette::ButtonText, Qt::black);
    UnknowBtn->setPalette(pal);
    UnknowBtn->setAutoFillBackground(true);
    UnknowBtn->setFlat(true);
    UnknowBtn->hide();

    initTabHead();
    initTabLabel();
}

void EquipmentStatus::initTabHead()
{
    headDevName = new QLabel(this);
    headDevName->setText(tr("设备编号"));
    headDevName->setFont(QFont(ZITI, 16, QFont::Bold));
    headDevName->setGeometry(QRect(10, 27, 100, 30));
    headDevName->setStyleSheet("color:white");
    headDevName->setAlignment(Qt::AlignHCenter);

    headnNoise = new QLabel(this);
    headnNoise->setText(tr("环境噪声"));
    headnNoise->setFont(QFont(ZITI, 16, QFont::Bold));
    headnNoise->setGeometry(QRect(110, 27, 100, 30));
    headnNoise->setStyleSheet("color:white");
    headnNoise->setAlignment(Qt::AlignHCenter);

    headDevId = new QLabel(this);
    headDevId->setText(tr("设备类型"));
    headDevId->setFont(QFont(ZITI, 16, QFont::Bold));
    headDevId->setGeometry(QRect(220, 27, 100, 30));
    headDevId->setStyleSheet("color:white");
    headDevId->setAlignment(Qt::AlignHCenter);

    headIp = new QLabel(this);
    headIp->setText(tr("IP地址"));
    headIp->setFont(QFont(ZITI, 16, QFont::Bold));
    headIp->setGeometry(QRect(330, 27, 100, 30));
    headIp->setStyleSheet("color:white");
    headIp->setAlignment(Qt::AlignHCenter);

    headVol = new QLabel(this);
    headVol->setText(tr("音量"));
    headVol->setFont(QFont(ZITI, 16, QFont::Bold));
    headVol->setGeometry(QRect(440, 27, 100, 30));
    headVol->setStyleSheet("color:white");
    headVol->setAlignment(Qt::AlignHCenter);

    headStatus = new QLabel(this);
    headStatus->setText(tr("设备版本"));
    headStatus->setFont(QFont(ZITI, 16, QFont::Bold));
    headStatus->setGeometry(QRect(550, 27, 100, 30));
    headStatus->setStyleSheet("color:white");
    headStatus->setAlignment(Qt::AlignHCenter);

    headStatus = new QLabel(this);
    headStatus->setText(tr("设备状态"));
    headStatus->setFont(QFont(ZITI, 16, QFont::Bold));
    headStatus->setGeometry(QRect(660, 27, 100, 30));
    headStatus->setStyleSheet("color:white");
    headStatus->setAlignment(Qt::AlignHCenter);
}

void EquipmentStatus::initTabLabel()
{
    QLabel *label;
    for (int i = 0; i < 63; i++)
    {
        label = new QLabel(this);
        label->setText(tr("*"));
        if (i % 7 == 3)
            label->setFont(QFont(ZITI, 11, QFont::Normal));
        else
            label->setFont(QFont(ZITI, 16, QFont::Normal));
        label->setGeometry(QRect(10 + (50 + 60) * (i % 7), 70 + (1 + 30) * (i / 7), 100, 30));
        label->setStyleSheet("color:white");
        label->setAlignment(Qt::AlignHCenter);
        statusLabelList.append(label);
    }
}

void EquipmentStatus::clearTabLabel()
{
    foreach (QLabel *label, statusLabelList)
    {
        label->setText(tr("*"));
        label->setStyleSheet("color:white");
    }
}

void EquipmentStatus::setTabLabel(QStringList devList, int page)
{
    const int row    = 9;
    const int column = 7;

    for (int i = 0; i < row; i++)
    {
        int devIndex = i + page * row;

        if (devIndex < devList.size())  //处理行
        {
            QString     dev       = devList.at(devIndex);
            QStringList devColumn = dev.split('#');

            for (int j = 0; j < column; j++)
            {
                QString columnText = devColumn.at(j);
                if (j < devColumn.size())  //处理列
                {
                    statusLabelList.at(i * column + j)->setText(tr(columnText.toUtf8().data()));
                    if (columnText == "在线")
                        statusLabelList.at(i * column + j)->setStyleSheet("background-color:green");
                    else if (columnText == "离线")
                        statusLabelList.at(i * column + j)->setStyleSheet("background-color:red");
                    else if (columnText == "占用")
                        statusLabelList.at(i * column + j)->setStyleSheet("background-color:blue");
                }
                else
                {
                    statusLabelList.at(i * column + j)->setText(tr("null"));
                }
            }
        }
    }
}
